package com.longrise.android.byjk.plugins.aboutme.personalInfo;

import android.text.Html;
import android.text.TextUtils;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.android.byjk.utils.PhoneUtils;
import com.longrise.common.datasource.remote.LoadDataManager;


/**
 * Created by godliness on 2017/3/23.
 *
 */

public class OpenLoginPresenter implements PersonalInfoContract.OpenPresenters {

    private PersonalInfoContract.OpenLoginView mView;
    private OpenLoginActivity mCxt;

    public OpenLoginPresenter(PersonalInfoContract.OpenLoginView view, OpenLoginActivity act) {
        mView = view;
        mCxt = act;
    }

    @Override
    public void init() {
        mView.setPhoneHint("手机号码");
        mView.setCodeHint("短信验证码");
        mView.setTitle("开通快捷登录");
        mView.setRightText("跳过");
    }

    private boolean isPass(String params) {
        if (TextUtils.isEmpty(params)) {
            mCxt.showToast(mCxt.getString(R.string.phonenum_isnull));
            return false;
        }
        if (!PhoneUtils.isPhoneNumber(params)) {
            mCxt.showToast(mCxt.getString(R.string.txsjh));
            return false;
        }
        return true;
    }

    @Override
    public void toGetCode(String phone) {
        if (!isPass(phone)) {
            return;
        }
//        if (phone.equals(UserInfor.getInstance().getUserphone())) {
//            DZZWTools.showToast(mCxt, getString(R.string.phone_bundle_xt), Toast.LENGTH_SHORT);
//            return;
//        }
        EntityBean bean = new EntityBean();
        bean.set("checktype", "3"); //3 开通手机号快捷登录
        bean.set("mobile", phone);
        bean.set("cardno", UserInfor.getInstance().getUsersfzh());
        EntityBean reqBean = new EntityBean();
        reqBean.set("bean", bean);
        mView.setCodeBtnStatus(false, mCxt.getString(R.string.getmoblieconfrimcode), "#666666");
        mView.isLoading(true);

        LoadDataManager.getInstance().callService("", UrlConstants.BaseUrl, "bbt_login_sMobileCheckMsgCode", reqBean, new LoadDataManager.OnRequestCallListener() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    EntityBean bean = (EntityBean) result;
                    String resultdesc = bean.getString("resultdesc");
                    mCxt.showToast(resultdesc);
                    parseMolieCode(bean);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished(String key, String service) {
                mView.isLoading(false);
            }

            @Override
            public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                mView.setVftoNorm();
                mCxt.showToast(mCxt.getString(R.string.nonetwork));
            }
        });
    }

    private void parseMolieCode(EntityBean result) {
        int resultstate = result.getInt("resultstate");
        if (resultstate == 1) {
            EntityBean resultBean = result.getBean("result");
            String issuccess = resultBean.getString("issuccess");
            if (issuccess.equals("1")) {
                mView.getTimer().start();
            } else {
                mView.setVftoNorm();
            }
        } else {
            mView.setVftoNorm();
        }
    }

    @Override
    public void confirm(String phone, String code) {
        if (!isPass(phone)) {
            return;
        }
        if (TextUtils.isEmpty(code)) {
            mCxt.showToast(mCxt.getString(R.string.singlecode_isnull));
            return;
        }
        EntityBean changeBean = new EntityBean();
        changeBean.set("cardno", UserInfor.getInstance().getUsersfzh());
        changeBean.set("mobile", phone);
        changeBean.set("mobilecode", code);
        final EntityBean reqBean = new EntityBean();
        reqBean.set("bean", changeBean);

        mView.setConfrimBtnState(false);
        mView.isLoading(true);

        LoadDataManager.getInstance().callService("", UrlConstants.BaseUrl, "bbt_login_openPersonMobileLogin", reqBean, new LoadDataManager.OnRequestCallListener() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    EntityBean resultBean = (EntityBean) result;
                    String desc = resultBean.getString("resultdesc");
                    mCxt.showToast(desc);
                    parseConfirm(resultBean);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished(String key, String service) {
                mView.setConfrimBtnState(true);
                mView.isLoading(false);
            }

            @Override
            public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                mCxt.showToast(mCxt.getString(R.string.nonetwork));
            }
        });
    }

    private void parseConfirm(EntityBean resultBean) {
        int resultstate = resultBean.getInt("resultstate");
        boolean isopenmobile = false;
        if (resultstate == 1) {
            EntityBean bean = resultBean.getBean("result");
            String isopenmobilelogin = bean.getString("isopenmobilelogin");
            if (isopenmobilelogin.equals("1")) {
                UserInfor.getInstance().setUserphone(bean.getString("userphone"));
                UserInfor.getInstance().setIsopenmobilelogin(isopenmobilelogin);
                isopenmobile = true;
            }
            mView.updateContentState();  //更新当前页面状态
//            parseIntegral(bean, isopenmobile);
        }
    }

    private void parseIntegral(EntityBean integral, boolean isopenmobile) {
        String infostate = integral.getString("infostate");
        String acquiredscore = integral.getString("acquiredscore");
        String remindwords = integral.getString("remindwords");
        if (infostate != null && infostate.equals("1")) {
//            MyIntegralDialogUtil.showIntegralDailog(mCxt, acquiredscore, remindwords, mView.getFrom());
        } else if (infostate != null && infostate.equals("0")) {
            mCxt.showToast(remindwords);
        } else {
            //已完成过
            mView.close(isopenmobile);
        }
    }

    @Override
    public void loginPhone() {
        //xkj 20170419
        String tips = "";
        String usersfzh = UserInfor.getInstance().getUsersfzh();
        String userName = UserInfor.getInstance().getUserName();
        if (TextUtils.isEmpty(usersfzh)) {//身份证号码为空
            tips = "用户账号：" + userName;
        } else {
            tips = "用户账号：" + usersfzh;
        }
        String phonenum = "手机号" + UserInfor.getInstance().getUserphone() + "可以登录保易健康";
        mView.setLoginPhone(Html.fromHtml("您已成功开通快捷登录方式<br>" + "<font color='#ff9900'>" + tips + "</font><br>" + phonenum));
//        StringBuilder descBuilder = new StringBuilder();
//        descBuilder.append("您已成功开通快捷登录方式\n");
//        descBuilder.append(tips);
//        descBuilder.append("手机号" + UserInfor.getInstance().getUserphone() + "登录保宝");

    }




}
